package com.jsmy.marketing.fg;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jsmy.marketing.R;
import com.jsmy.marketing.adapter.DataItemAdapter;
import com.jsmy.marketing.api.RetrofitManager;
import com.jsmy.marketing.bean.BaseBean;
import com.jsmy.marketing.bean.TXLBean;
import com.jsmy.marketing.common.Link;
import com.jsmy.marketing.utils.L;
import com.jsmy.marketing.utils.ToastUtil;
import com.jsmy.marketing.view.IndexBar;
import com.jsmy.marketing.view.IndexLayout;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import cn.aigestudio.datepicker.cons.DPMode;
import cn.aigestudio.datepicker.views.DatePicker;

/**
 * Created by PC_WLT on 2018/8/27.
 */

public class DataItemFragment extends BaseFragment implements DataFragment.OnDataFGListener{
    List<TXLBean.DataBean.ListBeanX.ListBean> listBeanList;

    DataItemAdapter dataItemAdapter;
    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    private LinearLayoutManager manager;
    @BindView(R.id.index_layout)
    IndexLayout indexLayout;
    @Override
    protected int resLayout() {
        return R.layout.fg_data_item_layout;
    }

    @Override
    protected void initData() {
        listBeanList = (List<TXLBean.DataBean.ListBeanX.ListBean>) getArguments().getSerializable("dataItemList");
        manager = new LinearLayoutManager(getContext());
        myRecycler.setLayoutManager(manager);
        dataItemAdapter = new DataItemAdapter();
        if (listBeanList.size() > 0) {
            dataItemAdapter.replaceData(listBeanList);
        }
        myRecycler.setAdapter(dataItemAdapter);
        dataItemAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                TXLBean.DataBean.ListBeanX.ListBean listBean = (TXLBean.DataBean.ListBeanX.ListBean) adapter.getItem(position);
                switch (view.getId()) {
                    case R.id.btn_add_listener_test:
                        showTestDialog(listBean);
                        break;
                }
            }
        });
//        initRightBar();
    }

    @Override
    public void update() {

    }
    public void setData(List<TXLBean.DataBean.ListBeanX.ListBean> list){
        listBeanList=list;
        if (listBeanList.size() > 0) {
            dataItemAdapter.replaceData(listBeanList);
        }
    }

    private void initRightBar() {
        List<String> heads = new ArrayList<>();
        for (TXLBean.DataBean.ListBeanX.ListBean listBean : listBeanList) {
            if (!heads.contains(listBean.getXm())) {
                heads.add(listBean.getXm());
            }
        }
        indexLayout.setIndexBarHeightRatio(0.9f);
        indexLayout.getIndexBar().setIndexsList(heads);
        indexLayout.setCircleTextColor(Color.RED);
        indexLayout.setCircleRadius(200);
        indexLayout.setCirCleTextSize(150);
        indexLayout.setCircleColor(ContextCompat.getColor(getContext(), R.color.circle_bg));
        indexLayout.getIndexBar().setIndexChangeListener(new IndexBar.IndexChangeListener() {
            @Override
            public void indexChanged(String indexName) {
                for (int i = 0; i < listBeanList.size(); i++) {
                    if (indexName.equals(listBeanList.get(i).getXm())) {
                        manager.scrollToPositionWithOffset(i, 0);
                        return;
                    }
                }
            }
        });
    }

    AlertDialog.Builder normalDialog;

    private void showTestDialog(final TXLBean.DataBean.ListBeanX.ListBean listBean) {

        normalDialog = new AlertDialog.Builder(getActivity(), AlertDialog.THEME_HOLO_LIGHT);
        normalDialog.setTitle("加入试听");
//        normalDialog.setMessage(listBean.getXm());
        normalDialog.setCancelable(false);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.add_listener, null);
        TextView tvName = view.findViewById(R.id.tv_add_listener_student);
        final TextView tvTime = view.findViewById(R.id.tv_choose_time);
        final DatePicker picker = view.findViewById(R.id.datepicker_date);
        final EditText edt_nr = view.findViewById(R.id.edt_add_listener_text);
        picker.setDate(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH) + 1);
        picker.setMode(DPMode.SINGLE);
        picker.setTodayDisplay(true);
        picker.setOnDatePickedListener(new DatePicker.OnDatePickedListener() {
            @Override
            public void onDatePicked(String date) {
                tvTime.setText(date);
                picker.setVisibility(View.GONE);
            }
        });
        tvTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (picker.getVisibility() == View.GONE) {
                    picker.setVisibility(View.VISIBLE);
                }
            }
        });
        tvName.setText(listBean.getXm());
        normalDialog.setView(view);
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String time = tvTime.getText().toString().trim();
                        String nr = edt_nr.getText().toString().trim();
                        if (TextUtils.isEmpty(nr)){
                            ToastUtil.showLong(getContext(),"请选择时间");
                            return;
                        }
                        if (TextUtils.isEmpty(time)){
                            ToastUtil.showLong(getContext(),"请填写试听内容");
                            return;
                        }
                        addListenerTester(listBean, time, nr, "");

                    }
                });
        // 显示
        normalDialog.show();

    }

    public void addListenerTester(TXLBean.DataBean.ListBeanX.ListBean listBean, String sj, String stnr, String bz) {
        Map<String, Object> map = new HashMap<>();
        map.put("lid", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        map.put("xsid", "" + listBean.getKid());
        map.put("sj", "" + sj);
        map.put("stnr", "" + stnr);
        map.put("bz", "" + bz);
        mPresenter.loadData(RetrofitManager.getInstance().insertxsst(map), Link.APP_insertxsst);
    }

    @Override
    public void refreshData(Object response, String position) {
        switch (position) {
            case Link.APP_insertxsst:
                BaseBean baseBean = (BaseBean) response;
                if (baseBean.getCode().equals("Y")) {
                    ToastUtil.showShort(getActivity(), "加入试听成功");
                } else {
                    ToastUtil.showShort(getActivity(), "加入试听失败");
                }
                break;
        }
    }


    @Override
    public void updateData(List<TXLBean.DataBean.ListBeanX.ListBean> mList) {
        if (mList.size() > 0) {
            listBeanList=mList;
            dataItemAdapter.replaceData(listBeanList);
            L.i(TAG,"update----");
        }
    }
}
